package com.yyd.designmodel.observer.impl;

import java.util.Observable;
import java.util.Observer;

/**
 * 
 * @Description：[ 实现系统的观察者接口 ]
 *
 * @ProjectName：[designmodel]
 * @PackageName：[com.yyd.designmodel.observer.impl.Reader]
 * @CreateDate：[2015-1-20  上午9:54:46] 
 * @Author：[yuyidi]
 */
public class Reader implements Observer {
    private String name;
    public Reader(String name) {
        super();
        this.name = name;
    }

    //读者可以关注某一位作者，关注则是将自己添加到作者的观察者列表中去
    public void subscribe(String writerName){
        WriterManager.getInstence().getWriter(writerName).addObserver(this);
    }    
    public void unsubscribe(String writerName){
        WriterManager.getInstence().getWriter(writerName).deleteObserver(this);
    }
    
    //当关注的作者发布新书时，会通知读者去看
    public void update(Observable o, Object arg) {
        if(o instanceof Writer){
            Writer writer = (Writer)o;
            System.out.println(name+"知道"+writer.getName()+"发布了新书《"+writer.getLastNovel()+"》，非要去看。");
        }
    }

}
